package com.ymatou.iapi.accounting.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.accounting.parameter.*;
import com.ymatou.iapi.accounting.service.*;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.AccountCallServiceV2;
import com.ymttest.database.model.Account;
import com.ymttest.database.sqlwapper.AccountWapper;

/**
 * 创建资金账户接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_CreateAccount {
	private static int user_id = 21000000;
	private static CreateAccountBean createaccountBean;
	private static CreateAccountCall createaccountCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("创建资金账户接口");
	}

	@Before
	public void caseUp() {
		createaccountBean = new CreateAccountBean();
		createaccountCall = new CreateAccountCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(createaccountCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_CreateAccount_001() {
		Logger.start(true, "用户创建资金账户成功");
		try {
			int accountType = 1;
			int currencyType = 1;
			int userId = AccountCallServiceV2.getNewUid();
			user_id = userId;
			createaccountBean.setAccountType(accountType);
			createaccountBean.setAppId("AutoTest");
			createaccountBean.setCurrencyType(currencyType);
			createaccountBean.setMemo("自动化测试专用");
			createaccountBean.setUserId(userId);
			createaccountCall.setData(createaccountBean);
			createaccountCall.callService();

			Logger.verifyEquals(0, createaccountCall.getStatus(), "验证返回状态为0,操作成功");
			if (createaccountCall.getStatus() == 0) {
				String aid = createaccountCall.getAccountId();
				Logger.comment("账户号:" + aid);
				List<Account> aids = new AccountWapper().selectAccountInfo(aid);
				Logger.verifyEquals(true, aids.size() > 0, "验证创建资金账户表数据创建成功");
				Logger.verifyEquals(userId, (Integer) aids.get(0).getUserId(), "验证数据库新建的账户用户id为:" + userId);
				Logger.verifyEquals(0, (Integer) aids.get(0).getStatus(), "验证数据库新建的账户状态为0可用");
				Logger.verifyEquals(accountType, (Integer) aids.get(0).getAccountType(), "验证数据库新建的账户类型为:"
						+ accountType);
				Logger.verifyEquals(currencyType, (Integer) aids.get(0).getCurrencyType(), "验证数据库新建的币种类型为:"
						+ currencyType);
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_CreateAccount_002() {
		Logger.start(false, "账号不存在(同一个用户id+不同的账户类型+不同的币种类型),创建账户成功");
		try {
			int userId = user_id;
			int accountType = 4;
			int currencyType = 4;
			createaccountBean.setAccountType(accountType);
			createaccountBean.setAppId("AutoTest");
			createaccountBean.setCurrencyType(currencyType);
			createaccountBean.setMemo("自动化测试专用");
			createaccountBean.setUserId(userId);
			createaccountCall.setData(createaccountBean);
			createaccountCall.callService();

			Logger.verifyEquals(0, createaccountCall.getStatus(), "验证返回状态为0,操作成功");
			if (createaccountCall.getStatus() == 0) {
				String aid = createaccountCall.getAccountId();
				Logger.comment("账户号:" + aid);
				List<Account> aids = new AccountWapper().selectAccountInfoByUserId(userId);
				List<Account> naid = new AccountWapper().selectAccountInfo(aid);
				Logger.verifyEquals(2, aids.size(), "验证创建资金账户表数据创建成功");
				Logger.verifyEquals(userId, (Integer) naid.get(0).getUserId(), "验证数据库新建的账户用户id为:" + userId);
				Logger.verifyEquals(0, (Integer) naid.get(0).getStatus(), "验证新建的账户状态为0可用");
				Logger.verifyEquals(accountType, (Integer) naid.get(0).getAccountType(), "验证数据库新建的账户类型为:"
						+ accountType);
				Logger.verifyEquals(currencyType, (Integer) naid.get(0).getCurrencyType(), "验证数据库新建的币种类型为:"
						+ currencyType);
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_CreateAccount_003() {
		Logger.start(false, "创建账号已存在(同一个用户id+同样货币类型),提示创建用户失败");
		try {
			int accountType = 1;
			int currencyType = 4;
			int userId = user_id;
			createaccountBean.setAccountType(accountType);
			createaccountBean.setAppId("AutoTest");
			createaccountBean.setCurrencyType(currencyType);
			createaccountBean.setMemo("自动化测试专用");
			createaccountBean.setUserId(userId);
			createaccountCall.setData(createaccountBean);
			createaccountCall.callService();

			Logger.verifyEquals(3, createaccountCall.getStatus(), "验证返回状态为3,创建失败");
			Logger.verifyEquals("创建账户失败！用户编号:" + userId + ",账户类型:" + accountType + ",货币类型:" + currencyType,
					createaccountCall.getMsg(), "验证提示信息,创建失败");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_CreateAccount_004() {
		Logger.start(false, "创建账号已存在(同一个用户id+同样账户类型),提示创建用户失败");
		try {
			int accountType = 4;
			int currencyType = 1;
			int userId = user_id;
			createaccountBean.setAccountType(accountType);
			createaccountBean.setAppId("AutoTest");
			createaccountBean.setCurrencyType(currencyType);
			createaccountBean.setMemo("自动化测试专用");
			createaccountBean.setUserId(userId);
			createaccountCall.setData(createaccountBean);
			createaccountCall.callService();

			Logger.verifyEquals(3, createaccountCall.getStatus(), "验证返回状态为3,创建失败");
			Logger.verifyEquals("创建账户失败！用户编号:" + userId + ",账户类型:" + accountType + ",货币类型:" + currencyType,
					createaccountCall.getMsg(), "验证提示信息,创建失败");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_CreateAccount_005() {
		Logger.start(false, "用户id存在,指定的账户类型不在配置表中,创建账户失败");
		try {
			int accountType = 12345;
			int currencyType = 1;
			int userId = AccountCallServiceV2.getNewUid();
			createaccountBean.setAccountType(accountType);
			createaccountBean.setAppId("AutoTest");
			createaccountBean.setCurrencyType(currencyType);
			createaccountBean.setMemo("自动化测试专用");
			createaccountBean.setUserId(userId);
			createaccountCall.setData(createaccountBean);
			createaccountCall.callService();

			Logger.verifyEquals(2, createaccountCall.getStatus(), "验证创状态码为2 创建失败");
			Logger.verifyIsContains("AccountType不能为空", createaccountCall.getMsg(), "验证错误提示消息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_CreateAccount_006() {
		Logger.start(false, "用户id,账户类型存在,指定的币种类型不在配置表中,创建账户失败");
		try {
			int accountType = 1;
			int currencyType = 0;
			int userId = AccountCallServiceV2.getNewUid();
			createaccountBean.setAccountType(accountType);
			createaccountBean.setAppId("AutoTest");
			createaccountBean.setCurrencyType(currencyType);
			createaccountBean.setMemo("自动化测试专用");
			createaccountBean.setUserId(userId);
			createaccountCall.setData(createaccountBean);
			createaccountCall.callService();

			Logger.verifyEquals(2, createaccountCall.getStatus(), "验证创状态码为2 创建失败");
			Logger.verifyIsContains( currencyType + " 在货币类型表中不存在！", createaccountCall.getMsg(), "验证错误提示消息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_CreateAccount_007() {
		Logger.start(false, "用户id为空,创建失败");
		try {
			int accountType = 1;
			int currencyType = 1;
			int userId = AccountCallServiceV2.getNewUid();
			createaccountBean.setAccountType(accountType);
			createaccountBean.setAppId("AutoTest");
			createaccountBean.setCurrencyType(currencyType);
			createaccountBean.setMemo("自动化测试专用");
			// createaccountBean.setUserId(userId);
			createaccountCall.setData(createaccountBean);
			createaccountCall.callService();

			Logger.verifyEquals(2, createaccountCall.getStatus(), "验证创状态码为2 创建失败");
			Logger.verifyIsContains("用户编号参数值不能为0", createaccountCall.getMsg(), "验证错误提示消息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_CreateAccount_008() {
		Logger.start(true, "账户类型为空,创建成功,账户类型默认为0");
		try {
			int currencyType = 1;
			int userId = AccountCallServiceV2.getNewUid();
			// createaccountBean.setAccountType(accountType);
			createaccountBean.setAppId("AutoTest");
			createaccountBean.setCurrencyType(currencyType);
			createaccountBean.setMemo("自动化测试专用");
			createaccountBean.setUserId(userId);
			createaccountCall.setData(createaccountBean);
			createaccountCall.callService();

			Logger.verifyEquals(0, createaccountCall.getStatus(), "验证返回状态为0,操作成功");
			if (createaccountCall.getStatus() == 0) {
				String aid = createaccountCall.getAccountId();
				Logger.comment("账户号:" + aid);
				List<Account> aids = new AccountWapper().selectAccountInfo(aid);
				Logger.verifyEquals(true, aids.size() > 0, "验证创建资金账户表数据创建成功");
				Logger.verifyEquals(userId, (Integer) aids.get(0).getUserId(), "验证数据库新建的账户用户id为:" + userId);
				Logger.verifyEquals(0, (Integer) aids.get(0).getStatus(), "验证数据库新建的账户状态为0可用");
				Logger.verifyEquals(1, (Integer) aids.get(0).getAccountType(), "验证数据库新建的币种类型默认为1");
				Logger.verifyEquals(currencyType, (Integer) aids.get(0).getCurrencyType(), "验证数据库新建的币种类型为:"
						+ currencyType);
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_CreateAccount_009() {
		Logger.start(true, "币种类型为空,创建成功,币种类型为1");
		try {
			int accountType = 1;
			int userId = AccountCallServiceV2.getNewUid();
			createaccountBean.setAccountType(accountType);
			createaccountBean.setAppId("AutoTest");
			// createaccountBean.setCurrencyType(currencyType);
			createaccountBean.setMemo("自动化测试专用");
			createaccountBean.setUserId(userId);
			createaccountCall.setData(createaccountBean);
			createaccountCall.callService();

			Logger.verifyEquals(0, createaccountCall.getStatus(), "验证返回状态为0,操作成功");
			if (createaccountCall.getStatus() == 0) {
				String aid = createaccountCall.getAccountId();
				Logger.comment("账户号:" + aid);
				List<Account> aids = new AccountWapper().selectAccountInfo(aid);
				Logger.verifyEquals(true, aids.size() > 0, "验证创建资金账户表数据创建成功");
				Logger.verifyEquals(userId, (Integer) aids.get(0).getUserId(), "验证数据库新建的账户用户id为:" + userId);
				Logger.verifyEquals(0, (Integer) aids.get(0).getStatus(), "验证数据库新建的账户状态为0可用");
				Logger.verifyEquals(accountType, (Integer) aids.get(0).getAccountType(), "验证数据库新建的账户类型为:"
						+ accountType);
				Logger.verifyEquals(1, (Integer) aids.get(0).getCurrencyType(), "验证数据库新建的币种类型默认为1");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}